﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadingManager : MonoSingleton<LoadingManager>
{
   
    public float LoadPrograss
    {
        get
        {
            float _prograss1 = curVo.LoadPrograss;
            float _prograss2 = preVo == null ? 1 : preVo.Releaseprograss;
            return _prograss1 * 0.5f + _prograss2 * 0.5f;
        }
    }
    private LoadVo curVo;
    private LoadVo preVo;
    private object cache;
    private Dictionary<string, LoadVo> SceneVoDic;
    public Transform TempParent { get; private set; }

    public override void OnInit()
    {
        if (Instance != null && Instance != this)
        {
            MonoBehaviour[] monos = gameObject.GetComponents<MonoBehaviour>();
            if (monos.Length > 1)
            {
                Destroy(this);
            }
            else
            {
                Destroy(gameObject);
            }
            return;
        }
        TempParent = new GameObject("TempParent").transform;

        TempParent.SetParent(transform);

        TempParent.localRotation = Quaternion.identity;
        TempParent.localScale = Vector3.one;
        TempParent.localPosition = Vector3.one * 5000;

        SceneVoDic = new Dictionary<string, LoadVo>();
        SceneVoDic.Add("LoadDemo1", new TestSceneVo1());
        SceneVoDic.Add("LoadDemo2", new TestSceneVo2());
        enabled = false;
    }

    public void StartLoadSence(string sceneName,object data = null)
    {
        cache = data;
        if (curVo != null)
        {
            if (curVo.State != SceneState.Idle)
            {
                preVo = curVo;
            }
            curVo = SceneVoDic[sceneName];
            if (preVo != null)
            {
                if (preVo.State != SceneState.Idle)
                {
                    preVo.DestroyData();
                }           
            }       
        }
        else
        {         
            curVo = SceneVoDic[sceneName];          
        }
        enabled = true;
    }

    public void CloseLoadScene()
    {
        if (curVo != null)
        {
            curVo.EndEnter();
        }
    }

    private void Update()
    {
        if (preVo != null)
        {
            if (preVo.State != SceneState.Idle || preVo.State != SceneState.Running)
            {
                preVo.Update();
            }
        }
        if (curVo != preVo)
        {
            if (preVo != null)
            {

                if (curVo.State == SceneState.Idle && preVo.State == SceneState.Idle)
                {
                    if (LoadingEvents.HasOpenLoad)
                        curVo.LoadData(cache);
                    else
                        LoadingView.Instance.OpenLoadingScene();
                }
            }
            else
            {
                if (curVo.State == SceneState.Idle)
                {               
                    if (LoadingEvents.HasOpenLoad)
                    {
                        curVo.LoadData(cache);
                    }
                    else
                        LoadingView.Instance.OpenLoadingScene();
                }
               
                
            }

            if (curVo.State != SceneState.Idle || curVo.State != SceneState.Running)
            {
                curVo.Update();
            }

        }
        else
        {
            if (preVo == null)
            {
                if (curVo.State != SceneState.Idle || curVo.State != SceneState.Running)
                {
                    curVo.Update();
                }
            }
            if (curVo.State == SceneState.Idle)
            {
                curVo.LoadData(cache);
            }
        }
        if (curVo.State == SceneState.Running)
        {
            enabled = false;
        }      
    }
}
